<?php
/*由脚本生成 powerBy--Reer*/

namespace app\common\manage;

use app\common\bean\GlobalBean;
use app\common\bean\ListMap;
use app\common\bean\UserBean;
use app\common\exception\MyException;
use app\common\model\UserModel;
use app\common\utils\ExitJsonUtil;
use app\common\utils\RASUtil;
use app\common\utils\TimeUtil;
use app\common\validate\UserValidate;
use think\Exception;

class UserManage extends BaseManage
{
    /**
     * @return UserManage
     */
    static function getInstance() {
        return parent::getInstance(); // TODO: Change the autogenerated stub
    }

    /**支付减余额
     * @param $uid   会员id
     * @param $money 要减的钱
     * @return UserBean
     * @throws Exception
     */
    public function reduceBlance($uid, $money) {
        $userBean = new UserBean();
        $userBean->setId($uid);
        $user = UserManage::getInstance()->getInfo($userBean);
        if ($money > $user->getBlance()) {
            ExitJsonUtil::getInstance()->exitData($user->getBlance(), 501, '余额不足');
        }
        UserModel::getInstance()->where([UserBean::$id => $uid])->setDec(UserBean::$blance, $money);
        return $user;
    }

    /** 解密用户令牌
     * @param $token
     * @return array
     * @throws \Exception
     */
    public function decryptToken($token) {
        if (!$token)
            return ['', ''];
        $result = RASUtil::getInstance()->decrypt($token);
        $result ?: MyException::throwMyException(50008);
        $result = json_decode($result, true);
        if ($result[GlobalBean::$expireTime] < time()) {
            MyException::throwMyException(50008);
        }
        if (isset($result[UserBean::$publicOpenId])) {
            return [$result[UserBean::$publicOpenId], $result[UserBean::$id]];
        }
        return [$result[UserBean::$appletOpenId], $result[UserBean::$id]];
    }

    /**获取分页数据
     * @param ListMap $listMap
     * @return mixed
     * @throws \Exception
     */
    public function getPaginatorList(ListMap $listMap) {
        // TODO: Implement getPaginatorList() method.
        $conditionMap = new ListMap();
        if ($timeArray = TimeUtil::getInstance()->dealBetweenTime($listMap)) {
            $conditionMap->setParameter(UserBean::$createdAt, $timeArray);
        }
        $conditionMap->setParameter(UserBean::$nickname, ['like', '%'.$listMap->getParameter(UserBean::$nickname).'%']);
        !$this->getParamBool($listMap, "orderFieldName") ?: $conditionMap->setOrder($listMap->getParameter('orderFieldName').SPACE.$listMap->getParameter('orderType'));
        $conditionMap->setParameter(UserBean::$status, $listMap->getParameter(UserBean::$status));
        $result = UserModel::getInstance()->getList($conditionMap);
        $result ?: MyException::throwMyException("获取失败");
        return $result;
    }

    /**添加数据
     * @param UserBean $userBean
     * @return mixed
     * @throws \Exception
     */
    public function add($userBean) {
        // TODO: Implement add() method.
        UserValidate::getInstance()->goCheck(UserValidate::$add, $userBean->getData());
        $userBean->setCreatedAt(TimeUtil::getInstance()->getTimeNow());
        $result = UserModel::getInstance()->addDataGetInsertId($userBean->getData());
        $result ?: MyException::throwMyException("添加失败");
        return $result;
    }

    /**编辑数据
     * @param UserBean $userBean
     * @return mixed
     * @throws \Exception
     */
    public function edit($userBean) {
        // TODO: Implement edit() method.
        UserValidate::getInstance()->goCheck(UserValidate::$pkId, $userBean->getData());
        $result = UserModel::getInstance()->updatedData(array(UserBean::$id => $userBean->getId()), $userBean->getData());
        $result !== false ?: MyException::throwMyException("修改用户信息出错");
        return $result;
    }

    /**删除数据 微信用户不删除
     * @param $data
     * @return mixed
     */
    public function delete($data) {
        // TODO: Implement delete() method.
        return true;
    }

    /**根据主建获取数据
     * @param UserBean $userBean
     * @return UserBean $userBean
     * @throws \Exception
     */
    public function getInfo($userBean) {
        UserValidate::getInstance()->goCheck(UserValidate::$pkId, $userBean->getData());
        $result = UserModel::getInstance()->selectData(array(UserBean::$id => $userBean->getId()), false, '', 'user_vip');
        $result ?: MyException::throwMyException("查询用户失败");
        $userBean->setData($result);
        return $userBean;
    }


}